module.exports = {
	/**
	 * 此函数名称
	 * @url user/pub/checkUser 前端调用的url参数地址
	 * data 请求参数 说明
	 * @params {String} uid  当前登录用户id,若用户已登录且token有效,则data中会带uid参数
	 * (此参数是后端过滤器通过token获取并添加到data中的,是可信任的)(只有kh函数才带此参数)
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, originalParam } = event;
		let { uniID, config, pubFun, vk , db, _ } = util;
		let { uid } = data;
		// 业务逻辑开始----------------------------------------------------------- 
		// 可写与数据库的交互逻辑等等
		let dbName = "vk-house";
    let sortArr = [{
    	"name": "_add_time",
    	"type": "desc"
    }];
        // price_sort 1 倒叙  正序
		 let {pageNum, pageSize, status, address_id, subway_value, price_sort,price_min, price_max} = data;
        if(price_sort == 1) {
          sortArr.push({
            "name": "salary",
            "type": "desc"
          })
        }
        if(price_sort == 2) {
          sortArr.push({
            "name": "salary",
            "type": "asc"
          })
        }
        const dbCmd = db.command
		 let whereJson = {};
     // 状态 1 上架 2审核 3 下架 4 删除
     if(status){
        whereJson["status"] = status;
     } else {
        whereJson['status'] = dbCmd.neq(4)
     }
		// 查询条件结束-----------------------------------------------------------
    if(address_id) {
    	whereJson["address_id"] = address_id;
    }
    if(subway_value) {
    	whereJson["subway_value"] = dbCmd.in(subway_value)
    }
    if (price_min && price_max) {
      whereJson['salary'] = dbCmd.and(dbCmd.gte(price_min), dbCmd.lte(price_max))
    } else {
      if (price_min) {
        whereJson['salary'] = dbCmd.lte(price_min)
      }
      if (price_max) {
        whereJson['salary'] = dbCmd.gte(price_max)
      }
    }
    
		let res = await vk.baseDao.select({
		  dbName:dbName,
		  pageKey:true,
		  getCount: true,
		  pageIndex:pageNum || 1,
		  pageSize:pageSize || 100,
		  whereJson:whereJson
		}, util);
    res.whereJson = whereJson
		return res;
	}
}